package com.xvyy.tingshu.config;


import com.xvyy.tingshu.aspect.CacheAspect;
import com.xvyy.tingshu.constant.CacheAbleConstant;
import com.xvyy.tingshu.service.impl.CacheOpsServiceImpl;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;


/**
 * ClassName: CacheAbleAutoConfiguration
 * Package: com.xvyy.tingshu.config
 *
 * @Description: redisson客户端配置类
 * @Author: xvyy
 * @Create: 2025/2/7 - 11:18
 * @Version: v1.0
 */
@Configuration
@Import(CacheOpsServiceImpl.class)
public class CacheAbleAutoConfiguration {

    @Autowired
    private RedisProperties redisProperties;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * redissonClient客户端配置
     *
     * @return
     */
    @Bean
    public RedissonClient redissonClient() {

        Config config = new Config();
        //单机模式 配置redis地址和密码
        config.useSingleServer().setAddress(CacheAbleConstant.REDIS_CACHE_PROTOCOL + redisProperties.getHost() + CacheAbleConstant.REDIS_CACHE_PROTOCOL_SPLIT + redisProperties.getPort()).setPassword(redisProperties.getPassword());
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }

    /**
     * 定义分布式布隆过滤器
     */

    @Bean
    public RBloomFilter rBloomFilter(RedissonClient redissonClient) {
        // 1.获取到布隆过滤器（第一次会创建一个新的 后面可以直接用）
        Boolean aBoolean = stringRedisTemplate.opsForValue().setIfAbsent(CacheAbleConstant.DISTRO_BLOOM_LOCK_KEY, CacheAbleConstant.DISTRO_BLOOM_LOCK_FLAG); // 主要保证幂等性（只有第一次才能完成布隆的初始化，后面服务不管重启多少次 都不会继续初始化）
        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(CacheAbleConstant.DISTRO_BLOOM_NAME);
        if (aBoolean) {
            // 2.初始化布隆过滤器
            bloomFilter.tryInit(CacheAbleConstant.DISTRO_BLOOM_SIZE, CacheAbleConstant.DISTRO_BLOOM_FPP);
            return bloomFilter;
        } else {
            // 3.返回布隆过滤器对象
            return bloomFilter;
        }
    }

    /**
     * 定义一个切面
     */
    @Bean
    public CacheAspect cacheAspect() {
        return new CacheAspect();
    }

}
